import { defineStore } from 'pinia'
import { ComponentInstance } from '@/types/visual-app'
// 定义字典项类型
interface DynamicComponentInstance {
  ref: any
  id:string
  componentId:string
  props:ComponentInstance
  [key: string]: any
}

export const useDynamicComponentStore = defineStore('dynamicComponent', () => {
  const refs = ref<Record<string, any>>({})

  function setRef(id: string, instance: DynamicComponentInstance) {
    if (instance) {
      refs.value[id] = instance
    } else {
      delete refs.value[id]
    }
  }

  function getRef(id: string) {
    return refs.value[id]
  }

  function clearRefs() {
    refs.value = {}
  }
  
  function removeRef(id:string) {
	delete refs.value[id]
  }
  return { refs, setRef, getRef, clearRefs,removeRef }
}) 